{include file='admin/before_body.phtml' title='后台首页'}
<style>
.user-avatar { border: 2px solid #c9d6e5;border-radius: 100%;max-width: 50px;margin-right: 10px;margin-left: 0;box-shadow: none; }
tbody .btn { margin-bottom: 2px }
</style>
  {include file='admin/header.phtml'}
    <div class="main-container" id="main-container">
      <script type="text/javascript">try { ace.settings.check('main-container' , 'fixed') }catch(e) {  }</script>
      <div class="main-container-inner">
        {include file='admin/sidebar.phtml'}
        <div class="main-content">
{$breadcrumbs=[]}
{$breadcrumbs[] = ['name' => '用户设置', 'icon' => 'icon-home home-icon']}
{$breadcrumbs[] = ['name' => '用户管理', 'isactive' => true]}
{include file='admin/breadcrumb.phtml' breadcrumbs=$breadcrumbs}
          <div class="page-content">
            {html_messages}
            <div class="row">
              <div class="col-sm-12">
                <div class="widget-box">
                  <div class="widget-header">
                    <h4>过滤条件</h4>
                  </div>
                  <div class="widget-body">
                    <div class="widget-main">
                      <form class="form-inline row">
                        <div class="col-sm-3">
                          <input type="text" name="search" value="{$query['search']}" class="form-control" placeholder="需要搜索的关键字">
                        </div>

                        <div class="col-sm-2">
                          <select name="status" class="form-control">
                            <option value=""  {if !is_numeric($query['status']) || !in_array($query['status'], [User::IS_EFFECTIVE, User::IS_FORBIDDEN])}selected="selected"{/if}>用户状态</option>
                            <option value="{User::IS_EFFECTIVE}" {if $query['status'] == User::IS_EFFECTIVE}selected="selected"{/if}>已启用</option>
                            <option value="{User::IS_FORBIDDEN}" {if is_numeric($query['status']) && $query['status'] == User::IS_FORBIDDEN}selected="selected"{/if}>已禁用</option>
                          </select>
                        </div>
                        <div class="col-sm-2">
                          <select id="roleid" name="roleid" class="form-control">
                            {if $role}<option value="{$role->role_id}">{$role->rolename}</option>{else}<option value=""></option>{/if}
                          </select>
                        </div>


                        <div class="col-sm-4">
                          <button type="subbit" class="btn btn-purple btn-sm">
                            查询
                            <i class="icon-search icon-on-right bigger-110"></i>
                          </button>
                        </div>
                      </form>
                    </div>
                  </div>
                </div>
              </div>
              <div class="col-xs-12"><div class="space-6"></div></div>

<div class="col-xs-12">
{if $account->checkPermission(['admin.edit'])}
  <p>
    <a href="{url('admin/user/add')}" class="btn btn-primary btn-sm"><i class="icon-plus align-top bigger-125"></i>新增用户</a>
    <button data-op="efective" class="batch btn btn-success btn-sm">批量启用</button>
    <button data-op="forbidden" class="batch btn btn-danger btn-sm">批量禁用</button>
  </p>
{/if}
  <div class="table-responsive">
    <div id="sample-table-2_wrapper" class="dataTables_wrapper" user="grid">
      <table id="sample-table-2" class="table table-striped table-bordered table-hover dataTable" aria-describedby="sample-table-2_info">
        <thead>
          <tr>
            <th class="center" width="50"><label><input type="checkbox" class="ace allCheckbox"/><span class="lbl"></span></label></th>
            <th class="center" width="50">头像</th>
            <th class="center">用户名</th>
            <th class="center">昵称</th>
            <th class="center" width="100">角色</th>
            <th class="center" width="50">状态</th>
            <th class="center" width="80">登陆次数</th>
            <th class="center" width="100">最后登陆时间</th>
            <th class="center" width="100">更新时间</th>
            {if $account->checkPermission(['admin.edit'])}
            <th class="center" width="120">操作</th>
            {/if}
          </tr>
        </thead>
        <tbody>
          {if empty($usersList)}
            <tr><td colSpan="10"><p class="center">找不到符合条件的记录<p></td></tr>
          {/if}
          {foreach $usersList as $user}
            <tr data-id="{$user->user_id}" data-username="{$user->username}">
              <td class="center">
                <label>
                  <input type="checkbox" class="ace checkboxval" value="{$user->user_id}" {if $user->isSuper()} disabled {/if} />
                  <span class="lbl"></span>
                </label>
              </td>
              <td class="center">
                <img class="user-avatar" src="{$user->avatar}">
              </td>
              <td class="center">{$user->username}</td>
              <td class="center">{$user->nickname}</td>
              <td class="center">
                {if $user->isSuper()}
                  系统管理员
                {else}
                  {$user->rolename|default:''}
                {/if}
              </td>
              <td class="center">
                {if $user->status === User::IS_FORBIDDEN}
                <span class="label label-sm label-danger arrowed arrowed-righ">已禁用</span>
                {elseif $user->status === User::IS_EFFECTIVE}
                <span class="label label-sm label-success arrowed arrowed-righ">已启用</span>
                {else}
                  {$user->status}
                {/if}
              </td>
              <td class="center">{$user->login_count}</td>
              <td class="center">{$user->last_login_time|date_format:'Y-m-d G:i:s'}</td>
              <td class="center">{$user->updated|date_format:'Y-m-d G:i:s'}</td>
              {if $account->checkPermission(['admin.edit'])}
                <td class="center">
                  {if $user->status == User::IS_FORBIDDEN}
                    <a class="btn btn-xs btn-success update" data-op="efective" {if $user->isSuper()} disabled {/if}>启用</a>
                  {elseif $user->status == User::IS_EFFECTIVE}
                    <a class="btn btn-xs btn-danger update" data-op="forbidden" {if $user->isSuper()} disabled {/if}>禁用</a>
                  {else}
                  {/if}
                  <a href="{url('admin/user/edit/')}{$user->user_id}" class="btn btn-xs btn-info" {if $user->isSuper()} disabled {/if}>编辑</a>
                  <a class="btn btn-xs btn-warning resetpassword" {if $user->isSuper()} disabled {/if}>重置密码</a>
                </td>
              {/if}
          </tr>
          {/foreach}
        </tbody>
      </table>
      <div class="row">
        <div class="col-sm-6">
        </div>
        <div class="col-sm-6">
          <div class="dataTables_paginate paging_bootstrap">
            {html_pagination page="$page" rows="$rows" count="$count" showinfo="1"
              url="{url('admin/user')}" query=$query}
          </div>
        </div>
      </div>
    </div>
  </div>
</div>



            </div>
          </div>
        </div>
      </div>
    </div>
</body>
{include file='admin/after_body.phtml'}
<script>

$('body').delegate('.update', 'click', function(e) {
  e.preventDefault();
  var _this = $(this), tr = _this.closest('tr'), userid = tr.attr('data-id'),
    username = tr.attr('data-username'), op = _this.attr('data-op'),
    opname = (op == 'forbidden' ? '禁用' : '启用'), context;
    context = '<p>您正在' + opname + '用户[ ' + username +' ]</p>' + (op == 'forbidden' ?
      '禁用用户后，该用户将无法登陆系统，请谨慎操作':
      '启用用户后，该用户可以登陆系统进程操作，请谨慎操作');
    confirm('<div class="message"></div><div class="context">' + context + '</div>', opname + '用户', function(a) {
      var _context = a.find('.context').html();
      var funShowError = function(message) {
        a.find('.message').html([
          '<div class="alert alert-danger" style=" padding: 5px;">',
          '<button type="button" class="close" data-dismiss="alert"><i class="icon-remove"></i></button>',
          message, '</div>'
        ].join(''));
        a.find('.context').html(_context);
        a.find('.modal-footer .btn-danger').removeClass('disabled');
        a.find('.modal-footer .btn-danger').text('重试');
      };
      var funSuccess = function(message) {
        a.find('.modal-footer .btn-danger').remove();;
        a.find('.context').html(message);
        a.on('hidden.bs.modal', function(e) {
          location.reload();
        });
      };
      var funUpdate = function() {
        a.find('.context').html('<i class="icon-spinner icon-spin orange bigger-125"></i> 正在提交申请，请稍等片刻...');
        $.ajax({
          type: 'POST', dataType: 'json',
          data: { userid: userid, op: op },
          url: "{url('admin/user/updateAjax')}",
        }).done(function(result) {
          if (result.code) {
            funShowError(result.msg);
          } else {
            funSuccess(opname + '用户[ ' + username + ' ]成功！！');
          }
        }).fail(function() {
          funShowError('服务器开了个小差，请重试！！');
        });
      };
      funUpdate();
    });
});

$('body').delegate('.batch', 'click', function(e) {
  e.preventDefault();
  var _this = $(this), op = _this.attr('data-op'),
    opname = (op == 'forbidden' ? '禁用' : '启用'),
    userids = [], count, context;
  $('.checkboxval:checked').each(function(){
    userids.push($(this).val());
  });
  count = userids.length;
  if (!count) {
    return alert('请选择要' + opname + '的用户！！', '提示');
  }
  context = '<p>您正在批量' + opname + '[ ' + count +  ' ]' + '个用户</p>' + (op == 'forbidden' ?
    '禁用选中用户后，所选的用户将将无法登陆系统，请谨慎操作':
    '启用选中用户后，所选的用户将可以登陆系统进程操，请谨慎操作');
    confirm('<div class="message"></div><div class="context">' + context + '</div>', opname + '用户', function(a) {
    var _context = a.find('.context').html();
    var funShowError = function(message) {
      a.find('.message').html([
        '<div class="alert alert-danger" style=" padding: 5px;">',
        '<button type="button" class="close" data-dismiss="alert"><i class="icon-remove"></i></button>',
        message, '</div>'
      ].join(''));
      a.find('.context').html(_context);
      a.find('.modal-footer .btn-danger').removeClass('disabled');
      a.find('.modal-footer .btn-danger').text('重试');
    };
    var funSuccess = function(message) {
      a.find('.modal-footer .btn-danger').remove();;
      a.find('.context').html(message);
      a.on('hidden.bs.modal', function(e) {
        location.reload();
      });
    };
    var funUpdate = function() {
      a.find('.context').html('<i class="icon-spinner icon-spin orange bigger-125"></i> 正在提交申请，请稍等片刻...');
      $.ajax({
        type: 'POST', dataType: 'json',
        data: { userids: userids, op: op, type: 'batch' },
        url: "{url('admin/user/updateAjax')}",
      }).done(function(result) {
        if (result.code) {
          funShowError(result.msg);
        } else {
          funSuccess('成功' + opname + '[ ' + result.result.affected + ' ]个用户');
        }
      }).fail(function() {
        funShowError('服务器开了个小差，请重试！！');
      });
    };
    funUpdate();
  });
});

$('body').delegate('.resetpassword', 'click', function(e) {
  e.preventDefault();
  var _this = $(this), tr = _this.closest('tr'), userid = tr.attr('data-id'),
    username = tr.attr('data-username'),
    context = '您正在重置用户[ ' + username + ' ] 密码，重置完成后，下次登陆生效';
    confirm('<div class="message"></div><div class="context">' + context + '</div>', '重置用户密码', function(a) {
      var _context = a.find('.context').html();
      var funshowMessage = function(message, success) {
        a.find('.message').html([
          '<div class="alert alert-' + (success ? 'success': 'danger') + '" style=" padding: 5px;">',
          '<button type="button" class="close" data-dismiss="alert"><i class="icon-remove"></i></button>',
          message, '</div>'
        ].join(''));
        a.find('.context').html(_context);
        a.find('.modal-footer .btn-danger').removeClass('disabled');
        a.find('.modal-footer .btn-danger').text(success ? '确定': '重试');
        a.on('hidden.bs.modal', function(e) {
          location.reload();
        });
      };
      var funUpdate = function() {
        a.find('.context').html('<i class="icon-spinner icon-spin orange bigger-125"></i> 正在提交申请，请稍等片刻...');
        $.ajax({
          type: 'POST', dataType: 'json',
          data: { userid: userid, type: 'resetpassword' },
          url: "{url('admin/user/updateAjax')}",
        }).done(function(res) {
          if (res.code) {
            funshowMessage(res.msg);
          } else {
            funshowMessage(res.result, true);
          }
        }).fail(function() {
          funshowMessage('服务器开了个小差，请重试！！');
        });
      };
      funUpdate();
    });
});

$('#roleid').select2(LIVE.component.select2.config({
    placeholder: '请选择一个角色',
    ajax: {
      url : '{url('admin/user/role')}',
      data: function(params) {
        return {
          search: params.term,
          page: params.page,
        };
      },
      processResults: function (res, params) {
        params.page = params.page || 1;
        var data = res.result;
        return {
          results: data.roles.map(function(role) {
            return { id: role.role_id, text: role.rolename }
          }),
          pagination: {
            more: data.next
          }
        };
      },
    },
    language: {
      'noResults': function(){
        return '<span>找不到相关的角色，<a href="{url('admin/user/role/add')}">添加角色</a></span>';
      }
    },
  }));

</script>
</html>

